package org.car.world.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.car.world.common.util.Page;
import org.car.world.core.entily.IssueTable;
import org.car.world.core.entily.MerchantsTable;
import org.car.world.service.IssueService;
import org.car.world.service.TypeService;
import org.hibernate.validator.constraints.EAN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Administrator
 * @ClassName: IssueController
 * @Description: 问题管理
 * @date Nov 13, 2018
 */
@Controller
@RequestMapping("admin")
public class IssueController {

    @Autowired
    TypeService typeservice;

    @Autowired
    IssueService issueservice;


    /**
     * @param @param  mo
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: doissuelist_method
     * @Description: 加载查看问题列表
     */
    @RequestMapping("doissuelist")
    public String doissuelist_method(@RequestParam(value = "form", required = false, defaultValue = "1") Integer form,
                                     @RequestParam(value = "istype", required = false, defaultValue = "0") Integer istype, Model mo) {

        Page<IssueTable> Page = new Page<IssueTable>();
        Page.setCurrentPage(form);
        Page.setPageSize(2);
        Page.setTotalRow(issueservice.count(istype));
        Page.setPageList(issueservice.selectIssueType(Page.getCurrentPage(), Page.getPageSize(), istype));
        mo.addAttribute("Page", Page);
        mo.addAttribute("istype", istype);
        mo.addAttribute("typelist", typeservice.selectAllidentify("TYPE_ISSUE"));
        return "issue_list";
    }


    /**
     * @Title: doissueadd_init @Description: 初始化访问添加方法 @param @return 参数 @return
     * String 返回类型 @throws
     */
    @RequestMapping("doissueadd")
    public String doissueadd_init(Model mo) {
        mo.addAttribute("typelist", typeservice.selectAllidentify("TYPE_ISSUE"));
        return "issue_add";
    }

    /**
     * @throws IOException
     * @throws IllegalStateException
     * @Title: doissueadd_init @Description: 添加数据 @param @return 参数 @return String
     * 返回类型 @throws
     */
    @RequestMapping("issueadddata")
    public String issueadddata_method(IssueTable iss, Model mo, MultipartFile a_logoPicPath,
                                      HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {
        try {

            int flag = 0;
            if (iss.getIssueType() == 2) {
                String path = null;// 文件路径
                String json = "";
                String Fpath = "";
                String fileName = "";
                if (!a_logoPicPath.isEmpty()) {
                    if (a_logoPicPath != null) {// 判断上传的文件是否为空
                        Fpath = "templates\\upload\\";
                        String type = null;// 文件类型
                        fileName = a_logoPicPath.getOriginalFilename();// 文件原名称
                        System.err.println("上传的文件原名称:" + fileName);
                        // 判断文件类型
                        type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
                        if (type != null) {// 判断文件类型是否为空
                            if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                                // 项目在容器中实际发布运行的根路径
                                String realPath = request.getSession().getServletContext().getRealPath("/");
                                // 自定义的文件名称
                                String trueFileName = fileName;
                                // 设置存放图片文件的路径
                                path = realPath + Fpath + trueFileName;
                                System.err.println("存放图片文件的路径:" + path);
                                // 转存文件到指定的路径
                                a_logoPicPath.transferTo(new File(path));
                                System.err.println("文件成功上传到指定目录下");
                            }
                            json = "{\"res\":1}";
                        } else {
                            System.out.println("不是我们想要的文件类型,请按要求重新上传");
                            json = "{\"res\":0}";
                        }
                    }
                }
                iss.setIssuePicture(Fpath + fileName);
            }

            flag = issueservice.insert(iss);
            if (flag > 0) {
                return "doissuelist";
            } else {
                return "doissueadd";
            }

        } finally {
            System.err.println("添加文件方法执行完毕！");
        }

    }

    /**
     * @param @param  issueid
     * @param @return 参数
     * @return Boolean    返回类型
     * @throws
     * @Title: doissueinfo_method
     * @Description:查看问题方法
     */
    @RequestMapping("doissueinfo")
    public String doissueinfo_method(Integer issueid, Model mo) {
        IssueTable issueTable = issueservice.selectByPrimaryKey(issueid);
        if (issueTable != null) {
            mo.addAttribute("IssueTable", issueTable);
            mo.addAttribute("typelist", typeservice.selectAllidentify("TYPE_ISSUE"));
        }
        return "issue_info";
    }

    /**
     * @param @param  issueid
     * @param @return 参数
     * @return Boolean    返回类型
     * @throws
     * @Title: doissuedel_method
     * @Description:删除方法
     */
    @RequestMapping("doissuedel")
    @ResponseBody
    public Boolean doissuedel_method(Integer issueid) {
        int flag = issueservice.deleteByPrimaryKey(issueid);
        if (flag > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param @param  issueid
     * @param @return 参数
     * @return Boolean    返回类型
     * @throws
     * @Title: doissuedel_method
     * @Description:访问更新的方法
     */
    @RequestMapping("doissueup")
    public String doissueup_init(Integer issueid, Model mo) {
        IssueTable issueTable = issueservice.selectByPrimaryKey(issueid);
        if (issueTable != null) {
            mo.addAttribute("IssueTable", issueTable);
            mo.addAttribute("typelist", typeservice.selectAllidentify("TYPE_ISSUE"));
        }
        return "issue_update";
    }


    /**
     * @param @param  issueid
     * @param @return 参数
     * @return Boolean    返回类型
     * @throws
     * @Title: doissuedel_method
     * @Description:删除图片方法
     */
    @RequestMapping("doissueupdatepic")
    @ResponseBody
    public Boolean doissueupdatepic_method(Integer upid) {
        int flag = issueservice.updateByPrimaryKeyPic(upid);
        if (flag > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param @param  issueid
     * @param @return 参数
     * @return Boolean    返回类型
     * @throws
     * @Title: doissuedel_method
     * @Description:更新方法
     */
    @RequestMapping("issueupdatedata")
    public String issueupdatedata_method(IssueTable iss, Model mo, MultipartFile a_logoPicPath,
                                         HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {
        try {

            int flag = 0;
            if (iss.getIssueType() == 2) {
                String path = null;// 文件路径
                String json = "";
                String Fpath = "";
                String fileName = "";
                if (!a_logoPicPath.isEmpty()) {
                    if (a_logoPicPath != null) {// 判断上传的文件是否为空
                        Fpath = "templates\\upload\\";
                        String type = null;// 文件类型
                        fileName = a_logoPicPath.getOriginalFilename();// 文件原名称
                        System.err.println("上传的文件原名称:" + fileName);
                        // 判断文件类型
                        type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
                        if (type != null) {// 判断文件类型是否为空
                            if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                                // 项目在容器中实际发布运行的根路径
                                String realPath = request.getSession().getServletContext().getRealPath("/");
                                // 自定义的文件名称
                                String trueFileName = fileName;
                                // 设置存放图片文件的路径
                                path = realPath + Fpath + trueFileName;
                                System.err.println("存放图片文件的路径:" + path);
                                // 转存文件到指定的路径
                                a_logoPicPath.transferTo(new File(path));
                                System.err.println("文件成功上传到指定目录下");
                            }
                            json = "{\"res\":1}";
                        } else {
                            System.out.println("不是我们想要的文件类型,请按要求重新上传");
                            json = "{\"res\":0}";
                        }
                    }
                }
                iss.setIssuePicture(Fpath + fileName);
            }

            flag = issueservice.updateByPrimaryKey(iss);
            if (flag > 0) {
                return "doissuelist";
            } else {
                return "doissueup";
            }

        } finally {
            System.err.println("更新方法执行完毕！");
        }

    }

}